#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include <sys/types.h>  
#include <sys/stat.h> 
#include <errno.h>

#include "util.h"

void updateSystemTime(UINT8 * rtcTime)
{
	char dateBuf[64];
	sprintf(dateBuf, "date -s \"%04d-%02d-%02d %02d:%02d:%02d\"", rtcTime[0]+2000, rtcTime[1], rtcTime[2], rtcTime[4], rtcTime[5], rtcTime[6]); //set YYYY-MM-DD
	//sprintf(dateBuf, "date -s \"2017-02-02 02:02:02\"");
	printf("updateSystemTime:%s\n", dateBuf);
	system(dateBuf);	
}

int createFilePath(const char *dirpath)
{
    struct stat file_stat;
    int ret;
    ret = stat(dirpath, &file_stat);
    if (ret < 0) {
        if (errno == ENOENT) {
            ret = mkdir (dirpath, 0775);
            //printf("creat dir \'%s\' \n", dirpath);
            if (ret < 0) {
               	printf("Could not create directory \'%s\' \n", dirpath);
                return -1;
            }

        } else {
            printf("bad file path\n");
            return -1;
        }
    }

    return 0;
}


void deleteFile(char * filepath)
{
	char dateBuf[128];
	sprintf(dateBuf, "rm %s", filepath);
	printf("delete %s\n", filepath);
	system(dateBuf);
}

void saveHexFile(char *filepath, UINT8 *hexstr, INT32 hexstrLen)
{
	if(hexstrLen > 0) {
		FILE* fd = fopen(filepath,  "wb");
		if(!fd) {
			printf("fopen failed\n");
			return;
		}
		fwrite(hexstr, 1, hexstrLen, fd);
		fclose(fd);
	}
	else {
		printf("saveTmpFile err.\n");
	}
}

int readHexFile(char *filepath, UINT8 *strhex, INT32 strhexLen)
{
	int ret;
	if(strhexLen > 0) {
		FILE* fd = fopen(filepath,  "rb");
		if(!fd) {
			printf("fopen failed\n");
			return -1;
		}
		ret = fread(strhex, 1, strhexLen, fd);
	}
	return ret;
}

void U32ToU8(UINT32 x, UINT8 *out)
{
	*out =  x>>24;
	*(out+1) = (x<<8)>>24;
	*(out+2) = (x<<16)>>24;
	*(out+3) = (x<<24)>>24;
}

UINT32 U8ToU32(UINT8 *input)
{
	return (*input<<24) | (*(input+1)<<16) | (*(input+2)<<8) | (*(input+3));
}

void U16ToU8(UINT16 x, UINT8 *out)
{
	*out =  x>>8;
	*(out+1) = (x<<8)>>8;
}

UINT16 U8ToU16(UINT8 *input)
{
	return ((*input<<8) | *(input+1));
}

static char Char2Hex(char c)
{
	if ((c >= 'a') && (c <= 'f'))
	{
		return c - 'a' + 10;
	}
	else if ((c >= 'A') && (c <= 'F'))
	{
		return c - 'A' + 10;
	}
	else if ((c >= '0') && (c <= '9'))
	{
		return c - '0';
	}
	else
	{
		return -1;
	}
}

int Str2Hex(char *pStr, char *pHex)
{
	char c0,c1;
	char *hp,*sp,*sep;

	if ((strlen(pStr) % 2) != 0)
	{
		return -1;
	}

	sep = pStr + strlen(pStr);
	hp = pHex;
	sp = pStr;
	while (sp < sep)
	{
		c0 = Char2Hex(sp[0]);
		c1 = Char2Hex(sp[1]);
		if ((c0 == (char)-1) || (c1 == (char)-1))
		{
			return -1;
		}
		hp[0] = (c0 << 4) + c1;
		sp += 2;
		hp ++;
	}

	return 0;
}

char *Hex2Str(char *pHex, char *pStr, int iHexLen)
{
	int i;

	for (i=0; i<iHexLen; i++)
	{
		sprintf(pStr+2*i, "%02X", (unsigned char)*(pHex+i));
	}
	return pStr;
}
